<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { editUserProfile } from '@/api/user.js'
import { formatDate } from '@/filters'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1800, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  methods: {
    async onConfirm (val) {
      const date = formatDate(val, 'YYYY-MM-DD')
      console.log(date)
      try {
        await editUserProfile({ birthday: date })
        this.$toast.success('更新生日成功')
        this.$emit('close')
        this.$emit('input', date)
      } catch (err) {
        this.$toast.success('更新失败,请稍后再试')
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
